package com.example.mychat.rabbitMq;

import com.example.mychat.entity.constants.Constants;
import com.example.mychat.webSocket.DelayedMessageListener;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @Description rabbitmq配置
 * @Author susu
 * @Date 2024/9/26
 */
@Configuration
@Slf4j
public class RabbitMqConfig {

    @Value("${mychat.queue.to:}")
    private String queueName;

    @Bean
    public Queue chatQueue(){
        return new Queue(queueName,true);
    }
    @Bean
    public TopicExchange chatExchange(){
        return new TopicExchange(Constants.EXCHANGE_CHAT_TOPIC_NAME);
    }
    @Bean
    public Binding chatBinding(Queue chatQueue,TopicExchange chatExchange){
        return BindingBuilder.bind(chatQueue).to(chatExchange).with(Constants.ROUTING_KEY_CHAT);
    }
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory){
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(messageConverter());
        return rabbitTemplate;
    }
    @Bean
    public SimpleMessageListenerContainer delayedContainer(ConnectionFactory connectionFactory, DelayedMessageListener delayedMessageListener){
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setMessageListener(delayedMessageListener);
        container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        container.setQueues(chatQueue());
        return container;
    }

    //测试用，对性能影响大
//    @PostConstruct
//    public void init(){
//        rabbitTemplate.setReturnsCallback(new RabbitTemplate.ReturnsCallback() {
//            @Override
//            public void returnedMessage(ReturnedMessage returnedMessage) {
//                log.error("监听到了消息return callback");
//                log.debug("exchange：{}",returnedMessage.getExchange());
//                log.debug("routingkey：{}",returnedMessage.getRoutingKey());
//                log.debug("message：{}",returnedMessage.getMessage());
//                log.debug("replyCode：{}",returnedMessage.getReplyCode());
//                log.debug("replyText：{}",returnedMessage.getReplyText());
//            }
//        });
//    }

}
